Add sam2 edit buttons#1308
Conversation
- Implemented SAM2 point segmentation tool allowing users to place positive and negative points to generate masks. - Added SAM2 bounding box segmentation tool for users to draw bounding boxes and generate masks. - Integrated SAM2 model loading and inference into the workflow generator. - Introduced new UI elements for mask padding configuration and clear mask functionality. - Added crosshair and rectangle images for cursor representation in the editor. - Enhanced image editor to handle mask application from generated images.
|
Before even properly reviewing this: I see code about downloading and running sam, but swarm already has sam2 handling in the image editor, so I wouldn't expect you to need to add new core execution handling for it? |
I forgot about that, I'll point the downloader to the existing functionality |
|
If any of the backend stuff needs to be moved just let me know, this was migrated from the extension and I tried not to modify any existing SAM2 classes until you confirmed you wanted it there |
|
ideally as much as possible existing functionality should be used and the new feature should be entirely in the UI, excluding only where the UI can't perform some part of it and some backend functionality is needed, in which case it should sit as close as possible to where the existing sam handling is |
… for improved maintainability
I consolidated the SAM2 downloader. The |
|
You still have entire large sections of custom sam handling in the python |
My mistake. I have broken out the |
|
simple image processing on the sam image should ideally be done on the browser (image editor code) not needing a call to python backend. Can just load the sam map once and operate on it continually |
If you are referring to The JavaScript simply constructs the points/bbox prompts and then renders the masks. |
PR Summary
This PR adds end-to-end SAM2 interactive masking support in SwarmUI, including frontend editor tools, backend workflow wiring, and custom Comfy nodes/utilities.
What Was Added
1. Backend parameter registration for SAM2 inputs
T2Iparams insrc/BuiltinExtensions/ComfyUIBackend/ComfyUIBackendExtension.cs:SAM2 Point ImageSAM2 Positive PointsSAM2 Negative PointsSAM2 BBoxSAM2 Mask Paddingsam2feature flag.2. Workflow generation steps for SAM2 masking
src/BuiltinExtensions/ComfyUIBackend/WorkflowGeneratorSteps.cs:Sam2Segmentationwith positive/negative coordinates.Sam2BBoxFromJson+Sam2Segmentation.sammaskpadding.SkipFurtherSteps = true) so mask generation acts as the terminal operation for that request.3. New Comfy extra node package for SAM2
src/BuiltinExtensions/ComfyUIBackend/ExtraNodes/Sam2BBoxNode/__init__.py:Sam2BBoxFromJsonnode (JSON bbox string ->BBOXtype).Sam2PointSegmentationnode (point-prompt segmentation).Sam2BBoxSegmentationnode (bbox-prompt segmentation).4. SAM2 utility implementation
src/BuiltinExtensions/ComfyUIBackend/ExtraNodes/Sam2BBoxNode/sam2.py:5. Frontend image editor tooling for SAM2
src/wwwroot/js/genpage/helpers/image_editor.js:ImageEditorToolSam2PointsImageEditorToolSam2BBoxonContextMenu) so right-click can be used for negative points.applyMaskFromImage(...)helper to apply generated masks to mask layers and normalize black pixels to transparent.6. New toolbar/tool icons
src/wwwroot/imgs/crosshair.pngsrc/wwwroot/imgs/rectangle.pngUser-visible impact